(function ($) {
	var $colledge = $('#colledge') // 学院控件
		,$course = $('#course') // 专业
		,$unit = $('#unit')   // 班级
		,$teacher = $('#teacher') // 教师
		,$graduate = $('#graduate'); // 毕业年份
	var unitUrl = '/public/unitof/' // 级联路径
		,teacherUrl = '/paper/user/findteacher/';
	
	var parentUnitId = "9999";//FIXME

	// 初始化加载功能
	var init = function () {
		$('#degree').combox();

		// 初始化毕业年份，当前年份先后推2年
		var date = new Date(), // 获得当前年,正常应该从服务器端获得时间。
			currentYear = date.getFullYear();
		var yearOptions = [];
		for (var i = 0; i <= 2; i++) {
			yearOptions.push('<option value="' + (currentYear + i) + '">' + (currentYear + i) + '</option>');
		}
		$graduate.html(yearOptions.join('')).combox();

		// 初始化教师，仅学生有用
		getTeachers(parentUnitId, function () {
			value = $teacher.attr('data-value');
			if (!value) return;
			$teacher.val(value);
		});
	};

	// 事件注册功能
	var regEvent = function () {
		$unit.click(function (e) {
			$('#element').toggle('fast');
			stopBubble(e);
		});

		$(document).click(function (e) {
			$('#element').hide('fast');
			stopBubble(e);
		});

		$('#element').click(function (e) {
			stopBubble(e);
		});
	};
 
	// 返回功能
	$('#back').click(function () {
		window.history.go(-1);
	});

	// 保存
	var save = function () {
		$('#my_form').postFormData().customSubmit(function () {
			var postData = $('#my_form').serialize()
				,resultData = getPostData(postData);
			resultData.unit = $unit.attr('data-value');
			$.post(keter.reqRoot + '/add', resultData, function (data) {
				if(data.message === 'SUCCESS'){
					alert('注册成功！请使用您的账户登录本系统。');
					window.location.href=keter.context;
				}
			});
		});
	};

	var getTreeInfo = function () {
		$.get(keter.context + unitUrl + '1', function (data) {
			var setting = {
				async: {
					enable: true,
					url : keter.context + unitUrl,
					autoParam:["id"]
				}
				,callback : {
					onClick : treeNodeClick
				}
			};
			$.fn.zTree.init($("#appTree"), setting, data);
		});
	};

	// 节点点击事件
	function treeNodeClick (e, treeId, treeNode, clickFlag) {
		stopBubble(e);
		var level = treeNode.level,
			parentNode = treeNode.getParentNode();
		if (level === 3) { // level3为班级
			$unit.val(treeNode.name).attr('data-value', treeNode.id);
			getTeachers(parentNode.id);
			$('#element').hide('fast');
		}
	}

	function getTeachers (id, callback) {
console.info(keter.context + teacherUrl + id)
		$.get(keter.context + teacherUrl + id, function (data) {
			if (data && data.length && data.length > 0) {
				var str = [];
				for (var i = 0, len = data.length; i < len; i++) {
					str.push('<option value="' + data[i].id + '">' + data[i].name + '</option>');
				}
				$teacher.html(str.join(''));
			} else {
				$teacher.html('<option value="oscar">本专业暂无导师</option>');
			}
			if (callback) callback();
		});
	}
	
	
	$.fn.getFormData = function (options) {
	var $data = options,
		$form = $(this),
		url = $form.attr('action');
		
	var defaultSubmit = function () {
		$form.submit(function (e) {
			stopDefault(e)
			form = $(this);
			var postData = getPostData(form.serialize());		
			$.post(url, postData, function(data){
				var result;
				if (data && data.responseJSON) {
					result = data.responseJSON.message;
				} else {
					result = data.message;
				}
				// 调整页面
				if (result === 'SUCCESS') {
					alert('注册成功！');
					window.location.href = keter.path + '/login';
				} else {
					alert(result);
				}
			});
		});	
	};
	
	return {
		defaultSubmit : defaultSubmit
	}
};
	
	var main = function () {
		init();
		getTreeInfo();
		regEvent();
		save();
	};

	return main();
})(jQuery);   

